﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using ARR.Entities;
using ARR.UI.UnitsOfMeasureService;
using DDF.Entity;

namespace ARR.UI.Models
{
    public interface IUnitsOfMeasureMasterModel : IModelBase
    {
        ObservableCollection<UnitOfMeasureConvertion> UnitsOfMeasure { get; set; }

        void Save();

        void LoadUnitsOfMeasure();
    }

    public class UnitsOfMeasureMasterModel : ModelBase, IUnitsOfMeasureMasterModel
    {
        public UnitsOfMeasureMasterModel()
        {
            UnitsOfMeasure = new ObservableCollection<UnitOfMeasureConvertion>();

            LoadUnitsOfMeasure();
        }

        public ObservableCollection<UnitOfMeasureConvertion> UnitsOfMeasure { get; set; }

        public void Save()
        {
            var toSave = UnitsOfMeasure.Where(x => x.GetEntityState() != EntityState.Original);

            UnitsOfMeasureServiceClient service = new UnitsOfMeasureServiceClient();
            try
            {
                service.Save(toSave.ToList());
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }

        }

        public void LoadUnitsOfMeasure()
        {
            UnitsOfMeasureServiceClient service = new UnitsOfMeasureServiceClient();
            try
            {
                UnitsOfMeasure = new ObservableCollection<UnitOfMeasureConvertion>(service.GetAll());
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }

        }
    }

    public class UnitsOfMeasureMasterModelMock : MockModelBase, IUnitsOfMeasureMasterModel
    {
        public UnitsOfMeasureMasterModelMock()
        {
            UnitsOfMeasure = new ObservableCollection<UnitOfMeasureConvertion>();
        }

        public ObservableCollection<UnitOfMeasureConvertion> UnitsOfMeasure { get; set; }

        public void Save()
        {
            
        }

        public void LoadUnitsOfMeasure()
        {
            UnitsOfMeasure.Add(new UnitOfMeasureConvertion { SourceUnit = "Gramos", TargetUnit = "Miligramos", Value = 1000m });
            UnitsOfMeasure.Add(new UnitOfMeasureConvertion { SourceUnit = "Miligramos", TargetUnit = "Gramos", Value = 1000m });
            UnitsOfMeasure.Add(new UnitOfMeasureConvertion { SourceUnit = "Litros", TargetUnit = "Botellas", Value = 2.25m });
            UnitsOfMeasure.Add(new UnitOfMeasureConvertion { SourceUnit = "Botellas", TargetUnit = "Litros", Value = 2.25m });
        }
    }
}
